#!/bin/bash

. /opt/module/rebate-report/configration/config.sh

result_url=${RESULT_URL}
result_database=${RESULT_DATABASE}
result_username=${RESULT_USERNAME}
result_password=${RESULT_PASSWORD}

if [ -n "$2" ] ;then
        echo "如果是输入的日期按照取输入日期"
        do_date=$2
else
        echo "====没有输入数据的日期，取当前时间的前一天===="
        do_date=`date -d yesterday +"%Y-%m-%d"`
fi
echo "日期为：$do_date"

#每天无分区全量
export_tb(){
sqoop export -D mapred.job.queue.name=root.tianyan \
--connect "jdbc:mysql://$result_url/$result_database?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false" \
--username  $result_username \
--password  $result_password \
--table $1 \
-m $2 \
--export-dir hdfs://nameservice:8020/warehouse/dw/$3 \
--fields-terminated-by '\001' \
--input-null-string '\\N' \
--input-null-non-string '\\N' \
--columns "$4"
}

#清空表
del_rebate_company_station="delete from app_fact_company_station where dt in (select partition_dt from app_rebate_partition where business_type = 1);"
del_rebate_company="delete from app_fact_company where dt in (select partition_dt from app_rebate_partition where business_type = 1);"
del_rebate_company_p="delete from app_fact_company_p where dt in (select partition_dt from app_rebate_partition where business_type = 1);"

export_app_rebate_company_station(){
mysql -h${result_url}  -P3306  -u${result_username} -p${result_password} ${result_database} -e  "${del_rebate_company_station}"
export_tb "app_fact_company_station" "1" "app/app_rebate_company_station" "group_id,station_id,fuel_type,amt_real,amt_rebate,dt"
}

export_app_rebate_company(){
mysql -h${result_url}  -P3306  -u${result_username} -p${result_password} ${result_database} -e  "${del_rebate_company}"
export_tb "app_fact_company" "1" "app/app_rebate_company" "group_id,rebate_rule,amt_recharge,amt_oil_recharge,amt_lng_recharge,amt_consume,amt_oil_consume,amt_lng_consume,amt_gasoline_consume,amt_other_consume,amt_profit,amt_oil_profit,amt_lng_profit,amt_gasoline_profit,amt_other_profit,amt_rebate,amt_oil_rebate,amt_lng_rebate,amt_real_profit,amt_oil_real_profit,amt_lng_real_profit,dt"
}

export_app_rebate_company_p(){
mysql -h${result_url}  -P3306  -u${result_username} -p${result_password} ${result_database} -e  "${del_rebate_company_p}"
export_tb "app_fact_company_p" "1" "app/app_rebate_company_p" "p_group_name,rebate_rule,amt_recharge,amt_oil_recharge,amt_lng_recharge,amt_consume,amt_oil_consume,amt_lng_consume,amt_gasoline_consume,amt_other_consume,amt_profit,amt_oil_profit,amt_lng_profit,amt_gasoline_profit,amt_other_profit,amt_rebate,amt_oil_rebate,amt_lng_rebate,amt_real_profit,amt_oil_real_profit,amt_lng_real_profit,dt"
}

case $1 in
"app_rebate_company_station")
export_app_rebate_company_station
;;
"app_rebate_company")
export_app_rebate_company
;;
"app_rebate_company_p")
export_app_rebate_company_p
;;
"all")
export_app_rebate_company_station
export_app_rebate_company
export_app_rebate_company_p
;;
esac